function formatar(src, mask){
    var i = src.value.length;
    var saida = mask.substring(0,1);
    var texto = mask.substring(i)
    if (texto.substring(0,1) != saida){
        src.value += texto.substring(0,1);
    }
}

function cpf_cnpj(){
    var cnpj = document.getElementById("doc_cnpj").checked;
    if(cnpj){
        document.getElementById("cpf").style.display = "none";
        document.getElementById("cnpj").style.display = "block";
        document.getElementById("dv_sexo").style.display = "none";
    }else{
        document.getElementById("cpf").style.display = "block";
        document.getElementById("cnpj").style.display = "none";
        document.getElementById("dv_sexo").style.display = "block";
    }
}

function mostra_sexo(doc){
    if(doc == 1){
        document.getElementById("dv_sexo").style.display = "none";
    }
}

function bg_menu(id){
    document.getElementById(id).style.background = "#e00";
    document.getElementById(id).style.color = "#fff";
}

 function seleciona_pagamento(v){
     location.href='?pag=vendaSet&fpg='+v;
 }
 
 function add_0(val){
    var p = 0;
    var str = val+'';
    var saida = '';
    
    for(var i = 0; i < str.length; i++){
        if(str.substring(i, i+1) == '.'){
            p = i;
        }
    }
    
    if(p != 0){
        if((p+2) == str.length){
            saida = str+'0';
        }else{
            saida = str;
        }
    }else{
        saida = str+".00";
    }
    
    return saida;
}

function add_p( val ){
    var a = '';
    for(var i = 0; i < val.length; i++){
        if(val.substring(i, i+1) == ','){
            a += '.';
        }else{
            a += val.substring(i, i+1);
        }
    }
    
    return a;
}

function limita_casa( val ){
    var p =0;
    for(i = 0; i < val.length; i++){
        if(val.substring(i, i+1) == '.'){
            p = i;
        }
    }
    if(p > 0){
        return val.substring(0, p+3);
    }else{
        return val;
    }
}

 function calc_venda_avista( d, t ){
    var r = 0;
    d = add_p(d);
    r = (t / 100) * d;
    r = parseFloat(t) - parseFloat(r);
    t = add_0(r);
    document.getElementById("total").value = limita_casa(t)
}

 function calc_venda_parcela( op, t ){
    var e = document.getElementById("entrada").value;
    var p = document.getElementById("parcelas").value;
    var j = document.getElementById("juros").value;
    var vp;
    if(t == null){
        t = document.getElementById("total").value;
    }
    
    if(p == 0){
        p = 1;
    }
    e = add_p(e);
    
    var tmp = parseFloat(t) - parseFloat(e);
    tmp = tmp / parseFloat(p);
    
    if(j != 0){
        vp = (parseFloat(tmp) / 100) * parseFloat(j);
        if(op == 1){
            vp = parseFloat(tmp) - parseFloat(vp);
        }else{
            vp = parseFloat(tmp) + parseFloat(vp);
        }
    }else{
        vp = tmp;
    }
    
    //Calcula Total
    var total = (parseFloat(p) * parseFloat(vp)) + parseFloat(e);
    
    document.getElementById("valorparcelado").value = limita_casa(add_0(vp));
    document.getElementById("total").value = limita_casa(add_0(total));
}

function limpa_codigo(id){
    document.getElementById(id).value = '';
}

function mostra_pagamento(){
    document.getElementById("conf_pagamento").style.display = "block";
}

function calc_troco(venda, pago, id){
    var result = parseFloat(add_p(pago)) - parseFloat(venda);
    result = limita_casa(add_0(result));
    document.getElementById(id).value = result;
}

function vendaPag(id){
    window.open('?pag=vendaPag&id='+id,'page','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=800,height=600');
}

function mostra_status(){
    document.getElementById("form_mostra_status").submit();
}

function mst_mnt(id){
    var ck = document.getElementById("mst_"+id).checked;
    if(ck == false){
        document.getElementById(id).style.display = "none";
    }else{
        document.getElementById(id).style.display = "block";
    }
}
